﻿// -----------------------------------------------------------------------
// <copyright file="ErrorCatcher.cs" company="Diddly Squad">
// </copyright>
// -----------------------------------------------------------------------

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DiddlySquat
{
	/// <summary>
	/// A way to display runtime errors when game is in release mode. Starts new Game with error msg screen. <see cref="Program"/> class.
	/// </summary>
	public class ErrorCatcher : Game
	{
		private const string ErrorTitle = "Unexpected Error";

		private const string ErrorMessage =
			"The game had an unexpected error and had to shut down. " + "We're sorry for the inconvenience.";

		private const string ContinueMessage = "Press any key to exit.";

		private readonly Exception exception;

		private SpriteBatch batch;

		private SpriteFont font;
		private SpriteFont errorTitleFont;

		/// <summary>
		/// Initializes a new instance of the <see cref="ErrorCatcher"/> class.
		/// </summary>
		/// <param name="e">The e.</param>
		public ErrorCatcher(Exception e)
		{
			new GraphicsDeviceManager(this) { PreferredBackBufferWidth = Constants.DefaultScreenWidth, PreferredBackBufferHeight = Constants.DefaultScreenHeight };
			exception = e;
			Content.RootDirectory = "Content";
		}

		/// <summary>
		/// Loads content for the class
		/// </summary>
		protected override void LoadContent()
		{
			batch = new SpriteBatch(GraphicsDevice);
			font = Content.Load<SpriteFont>("fonts/errorfont");
			errorTitleFont = Content.Load<SpriteFont>("fonts/errortitlefont");
		}

		/// <summary>
		/// Reference page contains links to related conceptual articles.
		/// </summary>
		/// <param name="gameTime">Time passed since the last call to Update.</param>
		protected override void Update(GameTime gameTime)
		{
			if (Keyboard.GetState().GetPressedKeys().Length > 0)
				Exit();
			base.Update(gameTime);
		}

		/// <summary>
		/// Reference page contains code sample.
		/// </summary>
		/// <param name="gameTime">Time passed since the last call to Draw.</param>
		protected override void Draw(GameTime gameTime)
		{
			GraphicsDevice.Clear(Color.Black);
			
				Vector2 p = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y);
				batch.Begin();
				batch.DrawString(errorTitleFont, ErrorTitle, p, Color.White);
				batch.DrawString(errorTitleFont, ErrorMessage, p + new Vector2(0,25), Color.White);
				batch.DrawString(font, exception.ToString(), p + new Vector2(0, 100), Color.White);
				batch.DrawString(font, exception.ToString(), p + new Vector2(0, 100), Color.White);
				batch.DrawString(font, ContinueMessage, p + new Vector2(GraphicsDevice.Viewport.Width / 2 - (int)font.MeasureString(ContinueMessage).X, GraphicsDevice.Viewport.Height - 25), Color.White);
				batch.End();

			base.Draw(gameTime);
		}
	}
}
